﻿using System;

namespace FrameDesign.FrameWork.Singleton
{
    public class Singleton<T> where T: class
    {
        public static T Instance
        {
            get
            {
                if (m_Instance == null)
                {
                    var ctors = typeof(T).GetConstructors(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                    var ctor = Array.Find(ctors, c => c.GetParameters().Length == 0);
                    if (ctor == null)
                    {
                        throw new Exception("Non-Public Constructor() not found in " + typeof(T));
                    }
                    m_Instance = ctor.Invoke(null) as T;
                }
                return m_Instance;
            }
        }

        private static T m_Instance;
    }
}